<?php
/*
 * @o: ========================================================
 * @Created By: VsCode
 * @Author: Fu
 * @name: 管理员控制器
 * @LastEditTime: 2021-05-12 09:43:18
 * @Date: 2021-04-02 18:13:40
 * @u: ========================================================
 */

namespace app\api\controller;

use app\admin\controller\goods\Goods;
use app\common\controller\Api;
use app\admin\model\Banner as BannerModel;
use app\admin\model\goods\Goods_category as GoodsCategoryModel;
use app\admin\model\goods\Product as ProductModel;
use app\admin\model\goods\Goods as GoodsModel;
use app\admin\model\Shopping as ShoppingModel;

use think\Db;
/**
 * 首页接口
 */
class Shopping extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @FunctionName:
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-12 08:48:48
     */
    public function addshop()
    {
        $param = input('param.');
        $arr = [];
        // $param = [
        //     0 => [
        //         'user_id' => 1,
        //         'goods_num' => 1,
        //         'goods_id' => 1,
        //         'goods_moeny' => 100,
        //     ],
        //     1 => [
        //         'user_id' => 1,
        //         'goods_num' => 2,
        //         'goods_id' => 1,
        //         'goods_moeny' => 100,
        //     ]
        // ];
        foreach($param as $key=>$value){
            $data['user_id'] = $value['user_id'];//用户ID
            $data['goods_num'] = $value['goods_num'];//商品数量
            $data['goods_id'] = $value['goods_id'];//商品ID
            $data['createtime'] = time();//创建时间
            $data['goods_moeny'] = $value['goods_moeny'] * $value['goods_num'];//商品总价
            $arr[] = $data;
        }
        $res = Db::name('shop_cart')->insertAll($arr);
        $this->success('成功',$res);
    }

    
    /**
     * @FunctionName: 
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-12 09:35:09
     */
    public function shoplist(ShoppingModel $ShoppingModel)
    {
        $param = input('param.');
        $shoplist = $ShoppingModel->with(['goods'],function($query){
            return $query->field('id,goods_name,goods_image')->find();
        })->where('user_id',$param['user_id'])->select()->toArray();
        // print_r($shoplist);exit;
        $this->success('请求成功',$shoplist);
    }

    /**
     * @FunctionName: 
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-12 09:43:17
     */
    public function delshop()
    {
        $param = input('param.');
        $id = explode(',',$param['id']);
        foreach($id as $key=>$value){
            $res = Db::name('shop_cart')->where('id',$value)->delete();
        }
        $this->success('成功',$res);
    }
}
